/*
 * TimerWindow.java
 *
 * Created on September 24, 2007, 12:53 PM
 */

package com.lct.eq2.gui;

import com.lct.eq2.gui.LiteGui.PieRenderer;
import com.lct.eq2.watchers.SpellTimerWatcher;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.ImageIcon;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

/**
 *
 * @author  waitek
 */
public class TimerWindow extends javax.swing.JFrame {
    private ArrayList timerThreads = null;
    private ArrayList swipes = null;
    private HashMap spellList = null;
    private String[] currentSpells = new String[4];
    private int spellCount = 0;
    
    /** Creates new form TimerWindow */
    public TimerWindow(HashMap list) {
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("eq2.png"));
        swipes = new ArrayList();
        spellList = new HashMap();
        timerThreads = new ArrayList();
        initComponents();
        this.setTitle("Spell Timers");
        this.spellList = list;
    }
    
    /** Creates new form TimerWindow */
    public TimerWindow() {
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("eq2.png"));
        swipes = new ArrayList();
        spellList = new HashMap();
        timerThreads = new ArrayList();
        initComponents();
        this.setTitle("Spell Timers");
    }
    
    public void setSpellList(HashMap map) {
        this.spellList = map;
    }
    
    public void startTimer(String spellName, double mult) {
        String[] tmp = (String[])this.spellList.get(spellName);
        if (Boolean.parseBoolean(tmp[2])) {
            this.currentSpells[spellCount] = spellName;
            this.spellCount++;
            SpellTimerWatcher watcher = new SpellTimerWatcher(Double.parseDouble(tmp[1]), tmp[0], this, spellCount, mult);
            timerThreads.add(watcher);
        }
    }
    
    public void addToTraumaticSwipes(String swiper, String target) {
        this.swipes.add(swiper);
        StringBuffer buf = new StringBuffer();
        for (int x = 0; x < swipes.size(); x++) {
            buf.append(swipes.get(x) + " has applied Traumatic Swipe to " + target + ".\n");
        }
        jLabel5.setText(buf.toString());
    }
    
    public void removeFromTraumaticSwipes(String swiper, String target) {
        this.swipes.remove(swiper);
        String label = jLabel5.getText();
        jLabel5.setText(label.replaceAll(label.substring(label.indexOf(swiper), label.indexOf(target) + target.length()), ""));
    }
    
    public void resetTimer(String spellName, double mult) {
        boolean found = false;
        for (int x = 0; x < currentSpells.length; x++) {
            if (spellName.equalsIgnoreCase(currentSpells[x])) {
                found = true;
                SpellTimerWatcher thread = (SpellTimerWatcher)timerThreads.get(x);
                double tmp = thread.getMultiplier();
                if (thread.getMultiplier() != mult) {
                    tmp = mult;
                }
                SpellTimerWatcher newWatcher = new SpellTimerWatcher(thread.getDelay(), thread.getSpellName(), 
                        thread.getParent(), thread.getSpellCountPosition(), tmp);
                thread.stopRunning();
                timerThreads.remove(x);
                timerThreads.add(x, newWatcher);
            }
        }
        if (!found) {
            this.startTimer(spellName, mult);
        }   
    }
    
    public void createTimerGraph(String timer, double remainingSeconds, double maxDelay, int spellCountPosition, boolean swiped) {
        DefaultPieDataset dataset = new DefaultPieDataset();
        
        double offset = (remainingSeconds / maxDelay);
        double percent = offset * 100;
        double remainder = 100 - percent;
        DecimalFormat df = new DecimalFormat("###");
        
        if (remainingSeconds < 1) {
            dataset.setValue("DUE ANY TIME", percent);
        } else {
            dataset.setValue(df.format(remainingSeconds) + "s", percent);
        }
        dataset.setValue("", remainder);
        
        JFreeChart chart = ChartFactory.createPieChart(
                timer, // chart title
                dataset,    // chart data
                false, 
                false, 
                false);
        PiePlot plot = (PiePlot)chart.getPlot();
       
        // Specify the colors here
        Color[] colors = new Color[2];
        colors[0] = Color.blue;
        if (remainingSeconds > 10) {
            colors[1] = Color.yellow;
        } else {
            swiped = false;
            colors[1] = Color.red;
        }
        PieRenderer renderer = new PieRenderer(colors, swiped);
        renderer.setColor(plot, dataset);
        plot.setDirection(Rotation.ANTICLOCKWISE);
        
        BufferedImage image = null;
        switch (spellCountPosition) {
            case 1:
                image = chart.createBufferedImage(jLabel1.getSize().width, jLabel1.getSize().height);
                jLabel1.setIcon(new ImageIcon(image));
                break;
            case 2:
                image = chart.createBufferedImage(jLabel2.getSize().width, jLabel2.getSize().height);
                jLabel2.setIcon(new ImageIcon(image));
                break;
            case 3:
                image = chart.createBufferedImage(jLabel3.getSize().width, jLabel3.getSize().height);
                jLabel3.setIcon(new ImageIcon(image));
                break;
            case 4:
                image = chart.createBufferedImage(jLabel4.getSize().width, jLabel4.getSize().height);
                jLabel4.setIcon(new ImageIcon(image));
                break;
            default:
                break;
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(layout.createSequentialGroup()
                            .add(10, 10, 10)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE))
                        .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 238, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 238, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 212, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 212, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        for (int x = 0; x < timerThreads.size(); x++) {
            SpellTimerWatcher watcherThread = (SpellTimerWatcher)timerThreads.get(x);
            watcherThread.stopRunning();
        }
    }//GEN-LAST:event_formWindowClosing
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables

}
